package com.yeyaomai.dksns.flowis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import com.yeyaomai.dksns.flowis.dao.FlowAuthDao;
import com.yeyaomai.dksns.flowis.domain.FlowAuth;
import com.yeyaomai.dksns.flowis.page.PageImplResult;
import com.yeyaomai.dksns.flowis.page.PageRequestOrder;
import com.yeyaomai.dksns.flowis.page.SplitConstant;
import com.yeyaomai.dksns.flowis.service.FlowAuthService;

@Service
public class FlowAuthServiceImpl implements FlowAuthService {

	@Resource
	private FlowAuthDao flowAuthDao;

	@Override
	public void addFlowAuth(FlowAuth flowAuth) {
		Integer authId = flowAuthDao.getMaxAuthId();
		flowAuth.setAuthId(authId+1);
		flowAuthDao.addFlowAuth(flowAuth);

	}

	@Override
	public void addFlowAuth_Role(FlowAuth flowAuth) {
		flowAuthDao.addFlowAuth_Role(flowAuth);

	}

	@Override
	public void updateFlowAuth(FlowAuth flowAuth) {
		flowAuthDao.updateFlowAuth(flowAuth);

	}

	@Override
	public void deleteFlowAuth(Long authId) {
		flowAuthDao.deleteFlowAuth(authId);
        flowAuthDao.deleteFlowAuth_RoleByAuthId(authId);
	}

	@Override
	public FlowAuth getFlowAuthByAuthId(Long authId)
	{
		return flowAuthDao.getFlowAuthByAuthId(authId);
	}

	@Override
	public Page<FlowAuth> listFlowAuth(Integer currentpage) {
		// TODO Auto-generated method stub
		Integer flowAuthCount =flowAuthDao.countFlowAuth();
		PageRequestOrder request = new PageRequestOrder(currentpage,
				SplitConstant.PAGE_SIZE);
		List<FlowAuth> flowAuthList = new ArrayList<FlowAuth>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("startNumber", request.start());
		map.put("endNumber", request.end());
		flowAuthList = flowAuthDao.listFlowAuth(map);
		Page<FlowAuth> result = new PageImplResult<FlowAuth>(flowAuthList,
				request, flowAuthCount);
		return result;
	}

	@Override
	public boolean isExistFlowAuth(String authName) {
		FlowAuth flowAuth = new FlowAuth();
		flowAuth = flowAuthDao.getFlowAuthByAuthName(authName);
		if (null == flowAuth) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public List<FlowAuth> listFLowAuthByStatus() {
		
		return flowAuthDao.listFlowAuthByStatus();
	}

	@Override
	public Map<Integer,Object> getFlowAuthByRoleId(Long roleId) {
		List<FlowAuth> checkAuthList = new ArrayList<FlowAuth>();
		checkAuthList = flowAuthDao.getFlowAuthByRoleId(roleId);
		FlowAuth flowAuth = null;
		Map<Integer,String> map = new HashMap<Integer, String>();
		Map<String,String> modelMap = new HashMap<String, String>();
		Map<Integer,Object> totalMap = new HashMap<Integer,Object>();
		for(int i = 0;i<checkAuthList.size();i++)
		{
			flowAuth = checkAuthList.get(i);
			map.put(flowAuth.getAuthId(), flowAuth.getAuthName());
			modelMap.put(flowAuth.getModelName(), flowAuth.getAuthName());
		}
		totalMap.put(1, map);
		totalMap.put(2, modelMap);
		return totalMap;
	}

	@Override
	public Map<Integer, String> getFlowAuthByRole(Long roleId,String userName) {
		List<FlowAuth> checkAuthList = new ArrayList<FlowAuth>();
		checkAuthList = flowAuthDao.getFlowAuthByRoleId(roleId);
		FlowAuth flowAuth = null;
		Map<Integer,String> map = new HashMap<Integer, String>();
		for(int i = 0;i<checkAuthList.size();i++)
		{
			flowAuth = checkAuthList.get(i);
			map.put(flowAuth.getAuthId(), flowAuth.getAuthName());
		}
		if(userName.equals("admin"))
		{
			map.remove(4);
		}
		return map;
	}

}
